<?php
namespace app\shop\controller;
use app\rbac\model\AdminM;
use app\shop\model\AttributeM;
use think\Db;
use think\Session;
use app\base\controller\Base;

class Attribute extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 新增
     * @return mixed
     */
    public function add($type_id)
    {

        //先判断权限
        $admin_id = session('admin_id');
        $adminModel = new AdminM();
        $res = $adminModel->isAdmin($admin_id);
        if($res === false)
        {
            $this->error('权限不够');
        }

        if($this->request->isPost())
        {
            $data = $this->request->post();
            $model = new AttributeM();
            $model->add($data);
            return json(['code'=>$model->code,'msg'=>$model->msg]);
        }
        $typeData = Db::table('shop_type')->select();
        $this->assign(array(
            '_page_title'=>'添加属性',
            '_page_title_english'=>'Attribute add',
            '_btn_title'=>'属性列表',
            '_btn_link'=>url('lst',['type_id'=>$type_id]),
            '_page_ico'=>'iconfa-screenshot',
            'type_id'=>$type_id,
            'typeData'=>$typeData,
        ));
        return $this->fetch();
    }



    /**
     * 编辑页面
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        //先判断权限
        $admin_id = session('admin_id');
        $adminModel = new AdminM();
        $res = $adminModel->isAdmin($admin_id);
        if($res === false)
        {
            $this->error('权限不够');
        }
        if($this->request->isPost())
        {
            $data = $this->request->post();
            $model = new AttributeM();
            $model->edit($data);
            return json(['code'=>$model->code,'msg'=>$model->msg]);
        }
        $typeData = Db::table('shop_type')->select();
        //获取数据
        $data = Db::table('shop_attribute')->where('id',$id)->find();

        $this->assign(array(
            '_page_title'=>'编辑属性',
            '_page_title_english'=>'Attribute edit',
            '_btn_title'=>'属性列表',
            '_btn_link'=>url('lst',['type_id'=>$data['type_id']]),
            '_page_ico'=>'iconfa-screenshot',
            'data'=>$data,
            'typeData'=>$typeData,
        ));
        return $this->fetch();
    }

    /**
     * 获取全部数据
     * @return mixed
     */
    public function lst($type_id)
    {
        //先判断权限
        $admin_id = session('admin_id');
        $adminModel = new AdminM();
        $res = $adminModel->isAdmin($admin_id);
        if($res === false)
        {
            $this->error('权限不够');
        }

        $model = new AttributeM();
        $data = $model->lst(15,$type_id);
        // 获取分页显示
        $page = $data->render();
        $this->assign(array(
            '_page_title'=>'属性列表',
            '_page_title_english'=>'Attribute list',
            '_btn_title'=>'添加属性',
            '_btn_link'=>url('add',['type_id'=>$type_id]),
            '_page_ico'=>'iconfa-screenshot',
            'data'=>$data,
            'page'=>$page,
        ));
        return $this->fetch();
    }


    /**
     * 删除数据
     * @param $id
     */
    public function del()
    {
        //先判断权限
        $admin_id = session('admin_id');
        $adminModel = new AdminM();
        $res = $adminModel->isAdmin($admin_id);
        if($res === false)
        {
            return json(['code'=>-1,'msg'=>'没有权限']);
        }
        $id = $this->request->post('id');
        $model = new AttributeM();
        $model->del($id);
        return json(['code'=>$model->code,'msg'=>$model->msg]);
    }
}
